/*
 * This file is part of the Sx Framework Library.
 * 
 * Copyright (C) 2013 University of Colorado Denver
 * <min.choi@ucdenver.edu> <shane.transue@ucdenver.edu>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 * copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 */
#ifndef SX_SIMULATION_H
#define SX_SIMULATION_H

#include <sxArrayList.h>
#include <sxTimer.h>

namespace Sx {
namespace Simulation {

/* 
 * Every simulation holds a list of listeners that are interested in the
 * events generated by a simulation. Additional functionality can then be
 * added to a simulation without all the additional features being implemented
 * in the simulation hierarchy.
 */
class SimulationListener;

/* Simulations can be based off of elapsed time or a fixed increment. */
enum TimeStepType { FIXED, REALTIME, STATIC };

class AbstractSimulation {
public:
	AbstractSimulation();
	virtual ~AbstractSimulation();

	static const double DEFAULT_TIMESTEP;
	static const TimeStepType DEFAULT_TIMESTEP_TYPE;

	virtual bool construct();

	virtual void preUpdate();
	virtual void update() = 0;
	virtual void postUpdate();

	virtual void preRender() const;
	virtual void render() const = 0;
	virtual void postRender() const;

	virtual void reset();

	void pause();
	void resume();
	void setPaused(bool pause);
	void setTimeStep(double timeStep);
	void setTimeStepType(TimeStepType type);

	bool isPaused() const;
	double getTimeStep() const;
	TimeStepType getTimeStepType() const;

	/* Pointer ownership is not transfered to the simulation. */
	bool addListener(SimulationListener* listener);
	bool removeListener(SimulationListener* listener);

protected:
	/* POINTER_OWNERSHIP: NO */
	Util::ArrayList<SimulationListener*> listeners;

	/* If the simulation is paused, no updates will occur. */
	bool paused;

	/* 
	 * The time-step defines the duration of each discrete step in the 
	 * simulation.
	 */
	double timeStep;

	/*
	 * The time-step type can either be a fixed type (controlled by a timer) or
	 * a 'realtime' calculated step (dt).
	 */
	TimeStepType timeStepType;

	/* Time-step counter. */
	unsigned int currentTimeStep;

	/* 
	 * Timer utilized to calculate the time between steps for realtime
	 * simulations.
	 */
	Util::Timer timer;
};

}

}

#endif
